import Loki from "lokijs";
export const db = new Loki("notes", {
  autoload: true,
  autoloadCallback: databaseInitialize,
  autosave: true,
  autosaveInterval: 3000,
  persistenceMethod: "localStorage"
});
function databaseInitialize() {
  // 获取notes这个集合
  const notes = db.getCollection("notes");
  // 判断如果notes这个集合是一个空对象
  if (notes == null) {
    //   则创建notes这个集合
    db.addCollection("notes");
  }
}
export function loadCollection(collection) {
  return new Promise(resolve => {
    db.loadDatabase({}, () => {
      const _collection =
        db.getCollection(collection) || db.addCollection("notes");
      resolve(_collection);
    });
  });
}
